<?php
//咨询类
class App_Chat extends Data_Base
{
    protected static $_systemIconUrl = '/resource/skin/img/index_mingxing.png';

	//构造咨询信息html
	//$msgId int 消息编号
    public static function buildMessageHtml($msgId)
    {
        if ($msgId <= 0)
            return '';

        $item = self::item('user_v2_chat', 'id', $msgId); 
        if ($item['fromUser'])
        {
            $user = User::getUserFromUid($item['uid']);
            $item['iconUrl'] = $user->getUserIcon();
        }
        else
        {
            $item['iconUrl'] = self::$_systemIconUrl;
        }

        $html = View::factory('smarty:app/chat/message')
            ->set('msg', $item)
            ->render();

        return $html;
    }

	//信息列表
	//$user 用户对象
    public static function infoList ($user, $guiderId="", $offset = 0, $limit = 10,$no=false)
    {
        $uid = $user->getUserUid();
        $userIcon = $user->getUserIcon();
        $cache = Cache::instance('file');
        $key = $uid.'-'.$guiderId;
        $data = $cache->get($key);
        $send_time = Arr::get($data,'last',time());
        if ($uid == 0)
            return array();
        if($guiderId != ""){
        $count = self::counts('user_v2_chat', array(array('uid', '=', $uid),array('guiderId','=',$guiderId),array('status', 'in', array(1,10))));
        }
        $query = DB::select()
            ->from('user_v2_chat')
            ->where('uid', '=', $uid)
            ;
        if($guiderId != ""){
            $query = $query ->where('status', 'in', array(1,10));
            $query = $query ->where('guiderId', '=', $guiderId);
        }
        if($no){
            $query =$query ->order_by('created', 'desc')
                ->execute();
        }else{
            $query =$query ->order_by('created', 'desc')->limit($limit)->offset($offset)->execute();
        }

        $userGuider = user::getUserFromUid($guiderId);
        $icon = $userGuider->getUserIcon();
        $ret = array();
        $time = $send_time;
        $query=array_reverse($query->as_array());

        foreach ($query as $rs)
        {
            if ($rs['fromUser'])
                $rs['iconUrl'] = $userIcon;
            else
                $rs['iconUrl'] = $icon;

            $abs_time = abs(strtotime(date('Y-m-d',$rs['created']))-strtotime(date('Y-m-d',$time)));
            if($abs_time>86400||$abs_time==86400){
               $rs['time']=date('m月d日H:i',$rs['created']);
            }else{
               $abs_time = abs($rs['created']-$time);
               if($abs_time>120)
               $rs['time']=date('H:i',$rs['created']);
            }
            $time = $rs['created'];
            $ret[] = $rs;
        }

        return array('total' => $count, 'list' => $ret);
    }

    public static function infoListAdmin ($user, $guiderId="", $offset = 0, $limit = 10,$no=false)
    {
        $uid = $user->getUserUid();
        $userIcon = $user->getUserIcon();
        $cache = Cache::instance('file');
        $key = $uid.'-'.$guiderId;
        $data = $cache->get($key);
        $send_time = Arr::get($data,'last',time());
        if ($uid == 0)
            return array();

        if($guiderId != ""){
        $count = self::counts('user_v2_chat', array(array('uid', '=', $uid),array('guiderId','=',$guiderId),array('status', 'in', array(1,10))));
        }
        $query = DB::select()
            ->from('user_v2_chat')
            ->where('uid', '=', $uid)
            ;
        if($guiderId != ""){
//            $query = $query ->where('status', 'in', array(1,20));
            $query = $query ->where('guiderId', '=', $guiderId);
        }
        if($no){
            $query =$query ->order_by('created', 'desc')
                ->execute();
        }else{
            $query =$query ->order_by('created', 'desc')//->limit($limit)->offset($offset)
                ->execute();
        }

        $userGuider = user::getUserFromUid($guiderId);
        $icon = $userGuider->getUserIcon();
        $ret = array();
        $time = $send_time;
        $query=array_reverse($query->as_array());

        foreach ($query as $rs)
        {
            if ($rs['fromUser'])
                $rs['iconUrl'] = $userIcon;
            else
                $rs['iconUrl'] = $icon;

            $abs_time = abs(strtotime(date('Y-m-d',$rs['created']))-strtotime(date('Y-m-d',$time)));
            if($abs_time>86400||$abs_time==86400){
               $rs['time']=date('m月d日H:i',$rs['created']);
            }else{
               $abs_time = abs($rs['created']-$time);
               if($abs_time>120)
               $rs['time']=date('H:i',$rs['created']);
            }
            $time = $rs['created'];
            $ret[] = $rs;
        }
        return array('total' => $count, 'list' => $ret);
    }

    public static function guiderInfoList ($user, $guiderId="", $offset = 0, $limit = 10)
    {
        $uid = $user->getUserUid();
        $userIcon = $user->getUserIcon();
        $cache = Cache::instance('file');
        $key = $guiderId.'-'.$uid;
        $data = $cache->get($key);
        $send_time = Arr::get($data,'last',time());
        if ($uid == 0)
            return array();

        if($guiderId != ""){
        $count = self::counts('user_v2_chat', array(array('uid', '=', $uid),array('guiderId','=',$guiderId),array('status', 'in', array(1,10))));
        }
        $query = DB::select()
            ->from('user_v2_chat')
            ->where('uid', '=', $uid);
        if($guiderId != ""){
            $query = $query ->where('status', 'in', array(1,10));
            $query = $query ->where('guiderId', '=', $guiderId);
        }

        $query =$query ->order_by('created', 'desc')->limit($limit)->offset($offset)
            ->execute();
        $userGuider = user::getUserFromUid($guiderId);
        $icon = $userGuider->getUserIcon();
        $ret = array();
        $time = $send_time;
        $query=array_reverse($query->as_array());

        foreach ($query as $rs)
        {
            if ($rs['fromUser'])
                $rs['iconUrl'] = $userIcon;
            else
                $rs['iconUrl'] = $icon;

            $abs_time = abs(strtotime(date('Y-m-d',$rs['created']))-strtotime(date('Y-m-d',$time)));
            if($abs_time>86400||$abs_time==86400){
               $rs['time']=date('m月d日H:i',$rs['created']);
            }else{
               $abs_time = abs($rs['created']-$time);
               if($abs_time>120)
               $rs['time']=date('H:i',$rs['created']);
            }
            $time = $rs['created'];
            $ret[] = $rs;
        } 
        return array('total' => $count, 'list' => $ret);
    }

    public static function guiderInfoListAdmin ($user, $guiderId="", $offset = 0, $limit = 10)
    {
        $uid = $user->getUserUid();
        $userIcon = $user->getUserIcon();
        $cache = Cache::instance('file');
        $key = $guiderId.'-'.$uid;
        $data = $cache->get($key);
        $send_time = Arr::get($data,'last',time());
        if ($uid == 0)
            return array();

        if($guiderId != ""){
        $count = self::counts('user_v2_chat', array(array('uid', '=', $uid),array('guiderId','=',$guiderId),array('status', 'in', array(1,10))));
        }
        $query = DB::select()
            ->from('user_v2_chat')
            ->where('uid', '=', $uid);
        if($guiderId != ""){
//            $query = $query ->where('status', 'in', array(1,10));
            $query = $query ->where('guiderId', '=', $guiderId);
        }

        $query =$query ->order_by('created', 'desc')//->limit($limit)->offset($offset)
            ->execute();
        $userGuider = user::getUserFromUid($guiderId);
        $icon = $userGuider->getUserIcon();
        $ret = array();
        $time = $send_time;
        $query=array_reverse($query->as_array());

        foreach ($query as $rs)
        {
            if ($rs['fromUser'])
                $rs['iconUrl'] = $userIcon;
            else
                $rs['iconUrl'] = $icon;

            $abs_time = abs(strtotime(date('Y-m-d',$rs['created']))-strtotime(date('Y-m-d',$time)));
            if($abs_time>86400||$abs_time==86400){
               $rs['time']=date('m月d日H:i',$rs['created']);
            }else{
               $abs_time = abs($rs['created']-$time);
               if($abs_time>120)
               $rs['time']=date('H:i',$rs['created']);
            }
            $time = $rs['created'];
            $ret[] = $rs;
        }
        return array('total' => $count, 'list' => $ret);
    }
	//编辑地址信息
	//$user	用户对象 
	//$nid 	地址编号
	//$data	数据信息
    public static function edit ($user, $nid, $data)
    {
        $row = self::buildDataFromRawInput($data);

        unset($row['id']);
        unset($row['uid']);
        unset($row['created']);
        $row['changed'] = DB::expr('unix_timestamp(now())');

        $rows = DB::update('user_v2_address')
            ->set($row)
            ->where('uid', '=', $user->getUserUid())
            ->where('id', '=', $nid)
            ->execute();

        return $rows > 0;
    }

	//添加消息
    public static function add ($user, $data)
    {
        $row = self::buildDataFromRawInput($data);

        unset($row['id']);
        $row['uid'] = $user->getUserUid();
        $row['created'] = DB::expr('unix_timestamp(now())');
        $row['guiderId'] = $data['guiderId'];
        $row['status'] = 1;

        list($insert_id, $rows) = DB::insert('user_v2_chat')
            ->columns (array_keys($row))
            ->values (array_values($row))
            ->execute();

        if ($insert_id > 0 && $row['fromUser'] == 1)
        {
            App_Account::setNewChatMessage($row['uid'], $row['message']);
        }
        else
        {
            App_Account::setNewChatMessage($row['uid'], NULL);
        }

        return $insert_id;
    }

	//删除地址
    public static function remove($user, $nid)
    {
        $rows = DB::delete('user_v2_address')
            ->where('uid', '=', $user->getUserUid())
            ->where('id', '=', $nid)
            ->limit(1)
            ->execute();

        return $rows > 0;
    }

	//构造添加消息格式
    protected static function buildDataFromRawInput ($data)
    {
        $ret = array();
        $ret['message']  = (String) Arr::get($data, 'message');
        $ret['fromUser'] = (int) Arr::get($data, 'fromUser');

        return $ret;
    }
}
